package com.kingint.web.sys.controller;


import com.kingint.common.unitedresult.K;
import com.kingint.mbg.annotation.SysLog;
import com.kingint.mbg.group.Insert;
import com.kingint.mbg.group.Update;
import com.kingint.mbg.sys.entity.UmsPermission;
import com.kingint.mbg.sys.entity.UmsRolePermissionRelation;
import com.kingint.web.util.validateUtil.ValidList;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.kingint.web.sys.service.UmsRoleService;
import com.kingint.mbg.sys.entity.UmsRole;

import java.util.List;


/**
 * @描述：后台用户角色表控制类
 * @作者: wxh
 * @日期: 2020-09-18
 */
@RestController
@RequestMapping("/umsRole")
@Api(value = "UmsRoleController", description = "后台用户角色表")
@CrossOrigin
public class UmsRoleController {

    @Autowired
    public UmsRoleService umsRoleService;


    @ApiOperation(value = "添加后台用户角色表")
    @PostMapping("/create")
    public Object create(@Validated(Insert.class) @RequestBody UmsRole umsRole) {
        Integer integer = umsRoleService.insertRoleInfo(umsRole);
        if (integer>0) {
            return K.ok().data(umsRole.getId());
        } else {
            return K.error();
        }
    }

    @ApiOperation(value = "更新后台用户角色表")
    @PutMapping("/update")
    public Object update(@Validated(Update.class) @RequestBody UmsRole umsRole) {
        Integer integer = umsRoleService.updateRoleInfo(umsRole);
        if (integer>0) {
            return K.ok().data(umsRole.getId());
        } else {
            return K.error();
        }
    }

    @ApiOperation(value = "删除后台用户角色表")
    @DeleteMapping("/delete/{id}")
    public Object delete(@PathVariable("id") Long id) {
        Integer integer = umsRoleService.deleteRoleInfo(id);
        if (integer>0) {
            return K.ok();
        } else {
            return K.error();
        }
    }

    @ApiOperation(value = "根据后台用户角色表名称分页获取后台用户角色表列表")
    @GetMapping("/list")
    public Object getList(UmsRole umsRole,
                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        IPage<UmsRole> roleList = umsRoleService.getRoleList(umsRole, pageNum, pageSize);
        return K.ok().data(roleList);
    }

    @ApiOperation(value = "根据编号查询后台用户角色表信息")
    @GetMapping("/{id}")
    public Object getItem(@PathVariable("id") Long id) {
        UmsRole umsRole = umsRoleService.getById(id);
        return K.ok().data(umsRole);
    }

    @ApiOperation("获取相应角色权限")
    @GetMapping(value = "/permission/{roleId}")
    public Object getPermissionList(@PathVariable Long roleId) {
        List<UmsPermission> permissionList = umsRoleService.getPermissionList(roleId);
        return K.ok().data(permissionList);
    }

    @SysLog(MODULE = "sys", REMARK = "获取相应角色权限-单表")
    @ApiOperation("获取相应角色权限-单表")
    @GetMapping(value = "/rolePermission/{roleId}")
    public Object rolePermission(@PathVariable Long roleId) {
        List<UmsRolePermissionRelation> rolePermission = umsRoleService.getRolePermission(roleId);
        return K.ok().data(rolePermission);
    }

    @ApiOperation("获取所有角色")
    @RequestMapping(value = "/getAllList", method = RequestMethod.GET)
    public Object list() {
        List<UmsRole> roleList = umsRoleService.getAllRoleList();
        return K.ok().data(roleList);
    }
}